﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ECCIT;
using ECCIT.Data;
using OgilvyOne.UKNow.Globalization;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.Utility;

namespace OgilvyOne.UKNow.DAL
{
    public class EventCalendarDAL : UKNowBaseDAL,IDALEventCalendar
    {
        private const string TBL_NAME = "UKNOW_MEMBER_EVENTS_MAPPING";
        private const string MEMBER_ID = "MEMBER_ID";
        private const string COL_EVENTS_ID = "COL_EVENTS_ID";
        private const string COL_LANG_ID = "COL_LANG_ID";
        public override string TableName { get { return TBL_NAME; } }
        public override Type ModelType { get { return typeof(MemberReminderMappingModel); } }
        [PrimaryKey]
        public IField LanguageID { get { return FieldFactory.Create(ModelType, "COL_LANG_ID", ColumnFactory.CreateColumn(COL_LANG_ID, DataType.Int)); } }
        [PrimaryKey]
        public IField Memberid { get { return FieldFactory.Create(ModelType, "MEMBER_ID", ColumnFactory.CreateColumn(MEMBER_ID, DataType.Int)); } }
        [PrimaryKey]
        public IField Eventid { get { return FieldFactory.Create(ModelType, "COL_EVENTS_ID", ColumnFactory.CreateColumn(COL_EVENTS_ID, DataType.Int)); } }
        public override IField[] InsertFields { get { return new IField[] { Memberid, Eventid,LanguageID }; } }
        public override IField[] SelectFields { get { return new IField[] { Memberid, Eventid, LanguageID }; } }
        public override IField[] UpdateFields { get { return new IField[] { Memberid, Eventid, LanguageID }; } }

         public EventCalendarDAL() { }

         public EventCalendarDAL(LanguageType languageType) { this.Language = LanguageFactory.Create(languageType); }

        public ResultInfo Save(EventCalendarModel modelInfo)
        {
            var info=new ResultInfo();
            if (CheckExist(modelInfo))
            {
                info.Message = Constants.IsExist;
                info.Result = false;
                return info;

            }
            if(Insert(modelInfo, null) > 0)
            {
                info.Message = Constants.Success;
                info.Result = true;
                return info;
            }
            return null;
        }

        public string GetLikeCountByEventID(int eid)
        {
            string db = "UKNOW_MEMBER_EVENTS_MAPPING";
            string whereCaluse = String.Format("select count( COL_EVENTS_ID) as ecount from {0} where COL_EVENTS_ID ={1} ",db, eid.ToString());
            string ecount=DBHelper.ExecuteScalar(whereCaluse).ToString();
            return ecount;


           
        }

        /// <summary>
        /// 取消收藏 2012-5-8
        /// </summary>
        /// <param name="modelInfo"></param>
        /// <returns></returns>
        public ResultInfo Delete(EventCalendarModel modelInfo)
        {
            ResultInfo info=new ResultInfo();
           
            string whereClause = string.Format("where {0}={1} and {2}={3} and {4}={5}", MEMBER_ID,
                                                modelInfo.MEMBER_ID, COL_EVENTS_ID, modelInfo.COL_EVENTS_ID,
                                                COL_LANG_ID, modelInfo.COL_LANG_ID);
            int result = DBHelper.ExecuteNonQuery("delete from  UKNOW_MEMBER_EVENTS_MAPPING  " + whereClause);
            if(result>0)
            {
                info.Result = true;
                info.Message = "1";
            }
            else
            {
                info.Result = true;
                info.Message = "-2";
            }
            return info;

        }

        public bool CheckExist(EventCalendarModel modelInfo)
        {

            string whereClause = string.Format("where {0}={1} and {2}={3} and {4}={5}", MEMBER_ID, modelInfo.MEMBER_ID, COL_EVENTS_ID, modelInfo.COL_EVENTS_ID, COL_LANG_ID,modelInfo.COL_LANG_ID);

            using (IDataReader reader = DBHelper.ExecuteReader("select count(1) from UKNOW_MEMBER_EVENTS_MAPPING " + whereClause))
            {
                while (reader.Read())
                {
                    return Convert.ToInt32(reader[0]) > 0;
                }
            }
            return false;

            //var model =
            //    (EventCalendarModel)
            //    GetModelByPrimaryKey(this.GetType(),
            //                         new KeyValuePair<IField, object>[]
            //                             {
            //                                 new KeyValuePair<IField, object>(this.Memberid, modelInfo.MEMBER_ID),
            //                                 new KeyValuePair<IField, object>(this.Eventid, modelInfo.COL_EVENTS_ID),
            //                                 new KeyValuePair<IField, object>(this.LanguageID, modelInfo.COL_LANG_ID)
            //                             });
            //return model != null;
        }
    }
}
